package com.ling.pl.security.controller;

import com.ling.pl.security.AuthorityType;
import com.ling.pl.security.model.ComponentDefinition;
import com.ling.pl.security.model.DefaultUrl;
import com.ling.pl.security.model.UrlComponent;
import com.ling.pl.security.service.DefaultUrlService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.util.*;

/**
 * @author bo.wang
 */
@RequestMapping("/defaultUrl")
@Api(value = "defaultUrl", description = "API")
@RestController("defaultUrlController")
public class DefaultUrlController extends BaseDefaultUrlController {

    Log log = LogFactory.getLog(DefaultUrlController.class);

    @Resource
    DefaultUrlService urlService;

    @ApiOperation(value = "刷新菜单缓存")
    @RequestMapping(value = "/refreshUrlCache", method = RequestMethod.GET)
    public void refreshUrlCache() {
        urlService.cacheNavigatorUrls();
    }

    @ApiOperation(value = "查询菜单")
    @RequestMapping(value = "/loadUrls", method = RequestMethod.GET)
    public Collection<DefaultUrl> loadUrls(@ApiParam(value = "parentId") @PathVariable(name = "parentId") String parentId) throws Exception {
        List<DefaultUrl> result = urlService.loadUrlsByParentId(parentId);
        return result;
    }

    @ApiOperation(value = "统计菜单数量")
    @RequestMapping(value = "/countChildren", method = RequestMethod.GET)
    public int countChildren(@ApiParam(value = "parentId") @PathVariable(name = "parentId") String parentId) {
        String hql = "select count(*) from " + DefaultUrl.class.getName() + " d where d.parentId = :parentId";
        Map params = new HashMap();
        params.put("parentId", parentId);
        return urlService.getDao().queryForInt(hql, params);
    }

    @ApiOperation(value = "查询某个菜单某个角色拥有的组件权限")
    @RequestMapping(value = "/loadUrlsByUrlIdAndroleId", method = RequestMethod.GET)
    public List<UrlComponent> loadUrlsByUrlIdAndroleId(@ApiParam(value = "urlId") @PathVariable(name = "urlId") String urlId, @ApiParam(value = "roleId") @PathVariable(name = "roleId") String roleId) {
        return urlService.loadUrlsByUrlIdAndroleId(urlId, roleId);
    }

    @ApiOperation(value = "新建UrlComponent关系")
    @RequestMapping(value = "/insertUrlComponents", method = RequestMethod.POST)
    @Transactional
    public void insertUrlComponents(Map<String, Object> parameter) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        String roleId = (String) parameter.get("roleId");
        String urlId = (String) parameter.get("urlId");
        map.put("roleId", roleId);
        map.put("urlId", urlId);
        List<UrlComponent> list = urlService.loadUrlsByUrlIdAndroleId(urlId, roleId);
        for (UrlComponent uc : list) {
            urlService.remove(uc.getComponent());
            urlService.remove(uc);
        }
        Collection<Map<String, Object>> components = (Collection<Map<String, Object>>) parameter.get("components");
        for (Map<String, Object> param : components) {
            String componentId = (String) param.get("id");
            String authorityType = (String) param.get("authorityType");
            ComponentDefinition component = new ComponentDefinition();
            component.setId(UUID.randomUUID().toString());
            component.setComponentId(componentId);
            UrlComponent uc = new UrlComponent();
            uc.setComponent(component);
            uc.setRoleId(roleId);
            uc.setUrlId(urlId);
            uc.setAuthorityType(AuthorityType.valueOf(authorityType));
            uc.setId(UUID.randomUUID().toString());
            urlService.save(uc);
        }
    }

    @ApiOperation(value = "从缓存中查询url树结构")
    @RequestMapping(value = "/loadMeunUrlsFromCache", method = RequestMethod.GET)
    public Collection<DefaultUrl> loadMeunUrlsFromCache(@PathVariable(name = "parentId") String parentId, String userName) {
        return urlService.loadMeunUrlsFromCache(parentId, userName);
    }

    @ApiOperation(value = "从缓存中查询子urls")
    @RequestMapping(value = "/loadContainChildMeunUrlsFromCache", method = RequestMethod.GET)
    public Collection<DefaultUrl> loadContainChildMeunUrlsFromCache(@PathVariable(name = "parentId") String parentId, String userName) {
        return urlService.loadContainChildMeunUrlsFromCache(parentId, userName);
    }

    @ApiOperation(value = "触发url缓存刷新")
    @RequestMapping(value = "/cacheNavigatorUrls", method = RequestMethod.GET)
    public void cacheNavigatorUrls() {
        urlService.cacheNavigatorUrls();
    }
}

